home *** CD-ROM | disk | FTP | other *** search
- procedure FindFile(initialPath : string; { initial path
- }
- fileMask : string; { mask to
- look For }
- recursive: boolean; { search
- subdirectories? }
- stopOnFirstMatch: boolean; { one match?
- }
- files: TStringList); { add
- match(es) to list }
- (* Starting at <initialPath>, FindFile will look for a match <fileMask>,
- if <Recursive> is True, all subdirectories beneath <initialPath> will
- be visited as well. If an initialPath is not given FindFile searches
- all non-removeable drives. Adds the paths where <fileMask> found to
- <files> *)
-
- type
- TAryDrive = array[0..25] of char;
- const
- aryDrive: TAryDrive = ('a', 'b', 'c', 'd' ,'e', 'f', 'g', 'h',
- 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
- 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
- 'y', 'z');
- var
- currentPath, currentDrive: string;
- i: byte;
-
- function IsDriveValid(drive: integer): boolean;
- { returns true if a valid drive }
- begin
- { not searching removable drives }
- Result := not (GetDriveType(drive) = DRIVE_REMOVABLE);
- if Result then begin
- ChDir(Format('%s:', [aryDrive[drive]]));
- Result := (IOResult = 0);
- end;
- end;
-
- procedure SearchDirectory(fileMask: string; path: TFileName);
- function MakePath(path, fileName: TFileName): TFileName;
- function AddSlashIfNeeded(path: string): string;
- begin
- Result := Path;
- if (not (path = '') and not (path[Length(path)] = '\')) then
- Result := Format('%s\', [Result]);
- end;
- begin
- Result := Concat(AddSlashIfNeeded(path), fileName);
- writeln(Format('Path = %s', [Result]));
- end;
- var
- searchRec: TSearchRec;
- stopped: boolean;
- begin
- { search the current directory for fileMask }
- stopped := False;
- try
- if FindFirst(MakePath(path, fileMask), faAnyFile, searchRec) = 0
- then
- repeat
- if searchRec.Attr <> faDirectory then begin
- files.Add(ExtractFilePath(MakePath(Path,
- searchRec.Name)));
- if stopOnFirstMatch then
- stopped := True
- end
- until (FindNext(searchRec) <> 0) or stopped;
- finally
- FindClose(searchRec);
- end;
-
- if recursive then
- {search the subdirectories for fileMask }
- try
- { Search current directory for subdirectories }
- if FindFirst(MakePath(path, '*.*'), faDirectory, searchRec) = 0
- then
- repeat
- with searchRec do
- if (Name <> '.') and (Name <> '..') and (Attr =
- faDirectory) then
- SearchDirectory(fileMask, MakePath(path, Name));
- { we have to be gentle to the others apps }
- Application.ProcessMessages;
- until (FindNext(searchRec) <> 0) or stopped;
- finally
- FindClose(searchRec);
- end;
- end;
- begin
- if initialPath <> '' then
- SearchDirectory(fileMask, initialPath)
- else begin
- { bookmark current drive and directory }
- GetDir(0, currentPath);
- currentDrive := Copy(currentPath, 1, 1);
-
- for i := 0 to High(aryDrive) do
- if IsDriveValid(i) then
- SearchDirectory(fileMask, Format('%s:\', [aryDrive[i]]));
-
- { reset to previous path }
- ChDir(Format('%s:', [currentDrive]));
- ChDir(currentPath);
- end;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- const
- fileMask = '*.dll';
- var
- files: TStringList;
- begin
- try
- { fill combo box with all paths that contain <filemask> }
- files := TStringList.Create;
- FindFile('', fileMask, True, False, files);
- comnboBox1.Items.Assign(files);
- finally
- files.Free;
- end;
- end;
-